package cn.afactory.tygz.ui.base.utils;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.text.TextUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import cn.afactory.tygz.net.Urls;

public class ImageUtils {
    static int MAX_UPLOAD_FILE_SIZE = 100 * 1024;

    public static String prepareUpload(String filePath) {
        String newFilePath;
        int scaleFactor;
        if (filePath == null) {
            return null;
        }

        File oldFile = new File(filePath);
        if (!oldFile.exists()) {
            return null;
        }

        if (oldFile.length() <= MAX_UPLOAD_FILE_SIZE) {
            return filePath;
        }
        newFilePath = DirConstants.DIR_PIC_ORIGIN + "/tmp.jpg";
        File newFile = new File(newFilePath);
        if (newFile.exists()) {
            newFile.deleteOnExit();
        }
        try {
            newFile.createNewFile();
            FileOutputStream fos = new FileOutputStream(newFile);
            scaleFactor = (int) (oldFile.length() / MAX_UPLOAD_FILE_SIZE);
            Options op = new Options();
            op.inSampleSize = scaleFactor;
            Bitmap newBitmap = BitmapFactory.decodeFile(filePath, op);
            newBitmap.compress(CompressFormat.JPEG, 80, fos);
            return newFilePath;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] Bitmap2Bytes(Bitmap bm) {
        if (bm == null) {
            return null;
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(CompressFormat.JPEG, 80, baos);
        return baos.toByteArray();
    }

    // byte[]转换成Bitmap
    public static Bitmap Bytes2Bitmap(byte[] b) {
        if (b.length != 0) {
            return BitmapFactory.decodeByteArray(b, 0, b.length);
        }
        return null;
    }

    /**
     * 获取图片的全路径
     *
     * @param imageName
     * @return
     */
    public static String getImageFullUrl(String imageName) {
        if (!TextUtils.isEmpty(imageName) && imageName.startsWith("http:")) {
            return imageName;
        }
        if (!TextUtils.isEmpty(imageName) && imageName.startsWith("file:")) return imageName;
        if (imageName == null) {
            return null;
        }
        String str = imageName.toLowerCase();
		if(str.endsWith(".gif")){
//			imageName = imageName.substring(0, imageName.length() - 4);
            return getThumbnailFullPathWithFormat(imageName, null);
        }
//        return Urls.URL_IMAGE_SUFFIX + imageName + "_1200x1200.png";
        return "";
    }

    /**
     * 拼出缩略图全地址
     * 类似：http://10.0.128.147:9080/smg/T1jaxTBCJT1RXrhCrK_300x300
     *
     * @param
     * @param sizeStr 大小 eg. “300x300”
     * @return url full path
     */
    public static String getThumbnailFullPath(String imageName, String sizeStr) {
        if (!TextUtils.isEmpty(imageName) && imageName.startsWith("http:")) {
            if (!imageName.contains("tfs")) {
                return imageName;
            }
            return imageName + "_" + sizeStr + ".png";
        }
//        return Urls.URL_IMAGE_SUFFIX + imageName + "_" + sizeStr + ".png";
        return "";
    }


    public static String getThumbnailFullPathWithFormat(String imageName, String sizeStr) {
        String format = ".gif";
        if (!TextUtils.isEmpty(imageName) && imageName.startsWith("http:")) {
            if (!imageName.contains("tfs")) {
                return imageName;
            }
            int index = imageName.lastIndexOf(".");
            if (index > 0) {
                format = imageName.substring(imageName.lastIndexOf("."), imageName.length());
            }
            return imageName;
        }
//        return Urls.URL_IMAGE_SUFFIX + imageName;
        return "";
    }

    public static String getPhotoFileName() {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss");
        return dateFormat.format(date) + ".jpg";
    }
}
